// Copyright (c) 2022 CHANGLEI. All rights reserved.

import 'package:aircraft_war/bloc/game_state_cubit.dart';
import 'package:aircraft_war/components/game_console.dart';
import 'package:flame/extensions.dart';

/// Created by changlei on 2022/5/23.
///
/// 游戏场景切换
class GameScene extends GameStatePositionComponent {
  GameConsole? _console;

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    this.size = size;
  }

  @override
  Future<void> onLoad() async {
    onNewState(gameRef.stateCubit.state);
  }

  @override
  void onNewState(GameState state) {
    switch (state) {
      case GameState.idle:
      case GameState.pending:
      case GameState.stopped:
        _console?.removeFromParent();
        _console = null;
        break;
      case GameState.running:
      case GameState.pausing:
        if (_console == null) {
          final console = GameConsole();
          _console = console;
          add(console);
        }
        break;
    }
  }
}
